<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" @reset="resetSearch">
      <el-col :span="8" :xs="24" v-show="$permiss[442]">
        <el-form-item :label="$t('所属租户')">
          <el-select v-model="page.adminUserId" @change="tenantChange" style="width: 100%" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[443]">
        <el-form-item :label="$t('APP名称')">
          <el-select style="width: 100%" v-model="page.productNames" multiple clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户类型')">
          <el-select style="width: 100%" v-model="page.isRepeatedBorrowing" clearable>
            <el-option v-for="item in userType" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('申请日期')">
          <el-date-picker
              v-model="page.timeValue"
              :clearable="false"
              style="width: 100%"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="~"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>
    <el-card shadow="never" style="margin-top: 10px">
      <Table :loading="loading" :page="page" :table-column="tableColumn" :table-data="tableData"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange"/>
    </el-card>
  </div>
</template>

<script>
import {dateFormatSection, defaultTimeDate} from "@/utils/tools";
import {orderApplyReport, orderApplyReportTotal} from '@/api/order'
import {selectProduct, selectTenant} from "@/api/system";
import {guestGroupList} from "@/api/postLoan";

export default {
  name: 'OrderReview',
  data() {
    return {
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 1,
        timeValue: defaultTimeDate(14)
      },
      tenantList: [],
      productList: [],
      tableData: [],
      tableColumn: [
        {label: this.$t('申请日期'), prop: 'time', minWidth: 100, fixed: 'left'},
        {label: this.$t('申请数'), prop: 'count',},
        {label: this.$t('机审通过数'), prop: 'machinePass', minWidth: 100,},
        {label: this.$t('机审通过率'), prop: 'machinePassRate', minWidth: 100,},
        {label: this.$t('机审拒绝数'), prop: 'machineRefuse', minWidth: 100,},
        {label: this.$t('机审拒绝率'), prop: 'machineRefuseRate', minWidth: 100,},
        {label: this.$t('人审通过数'), prop: 'pass', minWidth: 100,},
        {label: this.$t('人审通过率'), prop: 'passRate', minWidth: 100,},
        {label: this.$t('人审拒绝数'), prop: 'refuse', minWidth: 100,},
        {label: this.$t('人审拒绝率'), prop: 'refuseRate', minWidth: 100,},
        {label: this.$t('机审通过人审拒绝数'), prop: 'machinePassRefuse', minWidth: 150},
        {label: this.$t('机审通过人审拒绝率'), prop: 'machinePassRefuseRate', minWidth: 150},
        {label: this.$t('机审拒绝人审通过数'), prop: 'machineRefusePass', minWidth: 150},
        {label: this.$t('机审拒绝人审通过率'), prop: 'machineRefusePassRate', minWidth: 150},
        {label: this.$t('通过数'), prop: 'orderPass',},
        {label: this.$t('通过率'), prop: 'orderPassRate',},
        {label: this.$t('打款数'), prop: 'loanPay',},
        {label: this.$t('打款率'), prop: 'loanPayRate',},
      ],
      userType: [
        {label: this.$t('新客'), value: 0},
        {label: this.$t('老客'), value: 1},
      ],
      loading: false
    }
  },
  mounted() {
    this.getTableData()
    selectTenant().then(res => {
      this.tenantList = res.data
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
  },
  methods: {
    async getTableData() {
      this.loading = true
      const timeValue = dateFormatSection(this.page.timeValue)
      const params = Object.assign({}, this.page, {
        datetimeStart: timeValue && timeValue.length > 1 ? timeValue[0] : undefined,
        datetimeEnd: timeValue && timeValue.length > 1 ? timeValue[1] : undefined
      })
      delete params.timeValue
      const res = await orderApplyReport(params)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list
        await this.getTotal(params)
        this.page = {
          ...this.page,
          total: total
        }
        this.loading = false
      }
    },
    async getTotal(params) {
      const res = await orderApplyReportTotal(params)
      if (res.code === 1) {
        this.tableData.unshift({...res.data, time: this.$t('合计')})
      }
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    resetSearch() {
      this.page = {
        pageSize: 10,
        pageNum: 1,
        total: 0
      }
      this.getTableData()
    },
    tenantChange(value) {
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
        this.$set(this.page, 'productNames', [])
      })
    },
  }
}
</script>

<style scoped>

</style>
